/**
 * 通用工具
 */
const nodeXlsx = require('node-xlsx');

let commonUtil = {};

//处理oracle数据库查询list数据
commonUtil.handleOracleData = function (result) {
    //属性
    let metaData = [];
    result.data.metaData.forEach(x => {
        metaData.push(x.name);
    })
    let list = [];
    result.data.rows.forEach(x => {
        let entity = {};
        for (let i = 0; i < metaData.length; i++) {
            entity[metaData[i]] = x[i];
        }
        list.push(entity);
    })
    return list;
}

//通过excelPath获取指定sheetName的sheet数据
commonUtil.getSheetByPathAndSheetName = function (excelPath, sheetName) {
    // 解析表格
    let sheet = nodeXlsx.parse(excelPath);
    //获取sheet.data
    for (let i = 0; i < sheet.length; i++) {
        if (sheet[i].name === sheetName) {
            return sheet[i].data;
        }
    }
    return [];
}

//uuid
commonUtil.getUUID = function () {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    s[8] = s[13] = s[18] = s[23] = "-";
    return s.join("").replace(/-/g, '');
}

/**
 * 格式化excel时间，拿到的整数值是日期距离1900年1月1日的天数，类似于46254.1545151415
 * @param numb 传过来的数字
 * @param formatter 格式化：年-yyyy，月-MM，日-dd，时-HH，分-mm，秒-ss，毫秒-SSS
 * @param isHeadZero 月日时分秒等是否前面补0，如9->09
 */
commonUtil.formatExcelDate = function (numb, formatter, isHeadZero) {
    if (commonUtil.isEmpty(numb)) {
        return numb;
    }
    if (commonUtil.isEmpty(isHeadZero)) {
        isHeadZero = true;
    }
    const time = new Date((numb - 1) * 24 * 3600000 + 1);
    time.setFullYear(time.getFullYear() - 70);
    time.setHours(time.getHours() - 8);
    const year = time.getFullYear() + '';
    let month = time.getMonth() + 1 + '';
    let date = time.getDate() - 1 + '';
    let hours = time.getHours() + '';
    let minutes = time.getMinutes().toLocaleString();
    let seconds = time.getSeconds() + '';
    let milliseconds = time.getMilliseconds() + '';
    if (isHeadZero === true) {
        month = month.length === 1 ? '0' + month : month;
        date = date.length === 1 ? '0' + date : date;
        hours = hours.length === 1 ? '0' + hours : hours;
        minutes = minutes.length === 1 ? '0' + minutes : minutes;
        seconds = seconds.length === 1 ? '0' + seconds : seconds;
        milliseconds = milliseconds.length === 1 ? '00' + milliseconds : milliseconds.length === 2 ? '0' + milliseconds : milliseconds;
    }
    if (!formatter) {
        return year + month + date;
    }
    let formatTime = formatter.replace(/yyyy/g, year);
    formatTime = formatTime.replace(/MM/g, month);
    formatTime = formatTime.replace(/dd/g, date);
    formatTime = formatTime.replace(/HH/g, hours);
    formatTime = formatTime.replace(/mm/g, minutes);
    formatTime = formatTime.replace(/ss/g, seconds);
    formatTime = formatTime.replace(/SSS/g, milliseconds);
    return formatTime;
}

commonUtil.isEmpty = function (str) {
    return str === undefined || str === null || str.length === 0
}

commonUtil.isNotEmpty = function (str) {
    return !commonUtil.isEmpty(str);
}

commonUtil.equalsIgnoreCase = function (str1, str2) {
    return str1.toUpperCase() === str2.toUpperCase();
}

/**
 * 字符串前补0
 * @param str 字符串
 * @param num 0的个数
 */
commonUtil.addHeadZero = function (str, num) {
    if (!num) {
        return str;
    }
    for (let i = 0; i < num; i++) {
        str = '0' + str;
    }
    return str;
}

module.exports = commonUtil;
